import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class TabViewRoute extends StatefulWidget {
  @override
  _TabViewRouteState createState() => _TabViewRouteState();
}

class _TabViewRouteState extends State<TabViewRoute>
    with SingleTickerProviderStateMixin {
  late TabController _tabController;
  List tabs = ["新闻", "历史", "图片"];

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: tabs.length, vsync: this);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          "TabView",
          style: TextStyle(
            color: Colors.white, // 设置标题字体颜色
          ),
        ),
        iconTheme: IconThemeData(
          color: Colors.white, // 设置返回图标颜色
        ),
        backgroundColor: Colors.deepPurple, // 设置AppBar背景颜色
        bottom: TabBar(
          controller: _tabController,
          labelColor: Colors.white, // 选中标签的字体颜色
          unselectedLabelColor: Colors.grey, // 未选中标签的字体颜色
          labelStyle: TextStyle(fontWeight: FontWeight.bold), // 选中标签的字体样式
          unselectedLabelStyle: TextStyle(fontWeight: FontWeight.normal), // 未选中标签的字体样式
          indicatorColor: Colors.white, // 指示器颜色
          tabs: tabs.map((e) => Tab(text: e)).toList(),
        ),
      ),
      body: TabBarView(
        //构建
        controller: _tabController,
        children: tabs.map((e) {
          return Container(
            alignment: Alignment.center,
            child: Text(e, textScaleFactor: 5),
          );
        }).toList(),
      ),
    );
  }

  @override
  void dispose() {
    // 释放资源
    _tabController.dispose();
    super.dispose();
  }
}
